
package TransportesMarroquin;

import com.mysql.jdbc.Connection;

import java.awt.event.ActionEvent; 
import java.awt.event.ContainerEvent; 
import java.awt.event.KeyEvent; 
import javax.sound.sampled.Clip; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioSystem;
import javax.swing.JOptionPane;



public class NuevoGasto extends javax.swing.JFrame {

   
    int num;
    String tipov;
    javax.sound.sampled.Clip sonido;
    int total,dotr,dres,dgas,dace;
    int ano,mes,dia; // para ingresar la fecha 
    String fecha;
    int kilometraje=100;
    boolean error=false;
    public NuevoGasto() {  // contructor normal  
        initComponents();
         cgas.setEditable(false);
         cace.setEditable(false);
         cres.setEditable(false);
         cotr.setEditable(false);
         Fecha();

    }
    public NuevoGasto(int cod, String tipo){  // cuando tengo definido el codigo del furgon 
       
        initComponents();
         this.num=cod;
         this.tipov=tipo;
        codv.setText(""+num);
        codv.setEditable(false);
         cgas.setEditable(false);
         cace.setEditable(false);
         cres.setEditable(false);
         cotr.setEditable(false);
         Fecha();
    
    }
    public NuevoGasto(int cod, String tipo,int kil){  // cuando tengo definido el codigo del furgon 
       // gasto especial de los cabezales y camiones 
        initComponents();
         this.num=cod;
         this.tipov=tipo;
         this.kilometraje=kil;
        codv.setText(""+num);
        codv.setEditable(false);
         cgas.setEditable(false);
         cace.setEditable(false);
         cres.setEditable(false);
         cotr.setEditable(false);
         Fecha();
    
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        bace = new javax.swing.JRadioButton();
        bgas = new javax.swing.JRadioButton();
        cgas = new javax.swing.JTextField();
        bres = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        botr = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        btot = new javax.swing.JLabel();
        ctot = new javax.swing.JTextField();
        codv = new javax.swing.JTextField();
        cace = new javax.swing.JTextField();
        cres = new javax.swing.JTextField();
        cotr = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        cdes = new javax.swing.JTextArea();
        cdia = new javax.swing.JTextField();
        cmes = new javax.swing.JTextField();
        cano = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/cancelar.png"))); // NOI18N
        jButton3.setText("Cancerlar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/aceptar.png"))); // NOI18N
        jButton2.setText("Ingresar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        bace.setText("Aceite");
        bace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                baceActionPerformed(evt);
            }
        });

        bgas.setText("Diesel");
        bgas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bgasActionPerformed(evt);
            }
        });

        cgas.setText("0");
        cgas.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                cgasComponentAdded(evt);
            }
        });
        cgas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cgasActionPerformed(evt);
            }
        });
        cgas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cgasKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cgasKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cgasKeyTyped(evt);
            }
        });

        bres.setText("Repuesto");
        bres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bresActionPerformed(evt);
            }
        });

        jLabel3.setText("Descripción");

        botr.setText("Otros");
        botr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botrActionPerformed(evt);
            }
        });

        jLabel4.setText("Codigo Vehículo");

        btot.setText("Total");

        ctot.setEditable(false);
        ctot.setText("0");
        ctot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ctotActionPerformed(evt);
            }
        });

        codv.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                codvKeyTyped(evt);
            }
        });

        cace.setText("0");
        cace.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                caceComponentAdded(evt);
            }
        });
        cace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                caceActionPerformed(evt);
            }
        });
        cace.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                caceKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                caceKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                caceKeyTyped(evt);
            }
        });

        cres.setText("0");
        cres.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                cresComponentAdded(evt);
            }
        });
        cres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cresActionPerformed(evt);
            }
        });
        cres.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cresKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cresKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cresKeyTyped(evt);
            }
        });

        cotr.setText("0");
        cotr.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                cotrComponentAdded(evt);
            }
        });
        cotr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cotrActionPerformed(evt);
            }
        });
        cotr.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cotrKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cotrKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cotrKeyTyped(evt);
            }
        });

        cdes.setColumns(20);
        cdes.setLineWrap(true);
        cdes.setRows(5);
        jScrollPane1.setViewportView(cdes);

        cdia.setText("31");
        cdia.setBorder(null);

        cmes.setText("12");
        cmes.setBorder(null);

        cano.setText("2011");
        cano.setBorder(null);

        jLabel5.setText("/");

        jLabel6.setText("/");

        jLabel15.setText("Fecha");

        jPanel1.setBackground(new java.awt.Color(143, 0, 0));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Nuevo Gasto ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addContainerGap(426, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(376, 376, 376)
                .addComponent(jLabel15)
                .addGap(18, 18, 18)
                .addComponent(cdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addComponent(cmes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLabel6)
                .addGap(8, 8, 8)
                .addComponent(cano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel4)
                .addGap(27, 27, 27)
                .addComponent(codv, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel3)
                .addGap(36, 36, 36)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bgas)
                        .addGap(49, 49, 49)
                        .addComponent(bace)
                        .addGap(47, 47, 47)
                        .addComponent(bres)
                        .addGap(40, 40, 40)
                        .addComponent(botr))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cgas, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cace, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cres, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(cotr, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ctot, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btot))
                .addContainerGap(44, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(340, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(7, 7, 7)
                .addComponent(jButton3)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton2, jButton3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel15)
                    .addComponent(cdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(cmes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(cano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel4))
                            .addComponent(codv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(bgas))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(bace))
                            .addComponent(bres)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(botr)
                                .addComponent(btot)))
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cgas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cotr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(ctot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton2, jButton3});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        
        dispose();
    
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
error=false;
        try {
            Insertar();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NuevoCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(!error){
            if(tipov.equalsIgnoreCase("Viaje")){
                dispose();
                 Viaje v;
                 v = new Viaje("SELECT * FROM viaje");
                 v.setVisible(true);
                 v.setBounds(114, 107, Toolkit.getDefaultToolkit().getScreenSize().width-114, Toolkit.getDefaultToolkit().getScreenSize().height-107);
            }
            else {
                // Exitosa Entrada
            // sonido
                try
                {
                  sonido = null;
                  sonido=AudioSystem.getClip();
                  sonido.open(AudioSystem.getAudioInputStream(new File(System.getProperty ("user.dir")+"\\src\\SonidosPractica\\Correcto2.wav")));
                  sonido.start();




                }catch(Exception e)
                  {System.out.println(""+e);} // fin
                  JOptionPane.showMessageDialog(rootPane, "Dato ingresado exitosamente");
              // Oculta la Ventana
                   dispose();
           Gasto va;
   
            va = new Gasto("SELECT * FROM gasto WHERE tipovehiculo like '"+tipov+"' AND idvehiculo ="+num,num,tipov);
            va.setVisible(true);
            // centramos la pantalla
            va.setBounds(114, 107, Toolkit.getDefaultToolkit().getScreenSize().width-114, Toolkit.getDefaultToolkit().getScreenSize().height-107);
                  
            }
            
        }
          
}//GEN-LAST:event_jButton2ActionPerformed

    private void baceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_baceActionPerformed
        cace.setEditable(true);
        cace.setText("");
}//GEN-LAST:event_baceActionPerformed

    private void bgasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bgasActionPerformed
       cgas.setEditable(true);
       cgas.setText("");
}//GEN-LAST:event_bgasActionPerformed

    private void botrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botrActionPerformed
     cotr.setEditable(true);
     cotr.setText("");
}//GEN-LAST:event_botrActionPerformed

    private void cgasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cgasActionPerformed
   
       
    }//GEN-LAST:event_cgasActionPerformed

    private void cgasKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cgasKeyReleased
    
    
         try{
          dgas=Integer.parseInt(cgas.getText());
      }catch(Exception e ){
          
      } // fin del trye 
       TotalSuma();
      

       
    }//GEN-LAST:event_cgasKeyReleased

    private void cgasKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cgasKeyPressed
     
    }//GEN-LAST:event_cgasKeyPressed

    private void cgasComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_cgasComponentAdded
     
    }//GEN-LAST:event_cgasComponentAdded

    private void caceComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_caceComponentAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_caceComponentAdded

    private void caceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_caceActionPerformed
       
    }//GEN-LAST:event_caceActionPerformed

    private void caceKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_caceKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_caceKeyPressed

    private void caceKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_caceKeyReleased
  if(bace.isSelected()){
         cace.setEditable(true);
         try{
          dace=Integer.parseInt(cace.getText());
      }catch(Exception e ){
           } // fin del trye 
       TotalSuma();
      
    } // fin del if 
    }//GEN-LAST:event_caceKeyReleased

    private void cresComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_cresComponentAdded
       
    }//GEN-LAST:event_cresComponentAdded

    private void cresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cresActionPerformed

    }//GEN-LAST:event_cresActionPerformed

    private void cresKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cresKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_cresKeyPressed

    private void cresKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cresKeyReleased
      if(bres.isSelected()){
         cres.setEditable(true);
         try{
          dres=Integer.parseInt(cres.getText());
      }catch(Exception e ){
      } // fin del trye 
       TotalSuma();
      
    } // fin del if 
    }//GEN-LAST:event_cresKeyReleased

    private void cotrComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_cotrComponentAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_cotrComponentAdded

    private void cotrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cotrActionPerformed

    }//GEN-LAST:event_cotrActionPerformed

    private void cotrKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cotrKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_cotrKeyPressed

    private void cotrKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cotrKeyReleased
        if(botr.isSelected()){
         cotr.setEditable(true);
         try{
          dotr=Integer.parseInt(cotr.getText());
      }catch(Exception e ){
      } // fin del trye 
       TotalSuma();
      
    } // fin del if 
    }//GEN-LAST:event_cotrKeyReleased
public void TotalSuma (){
   dotr=Integer.parseInt(cotr.getText());
   dres=Integer.parseInt(cres.getText());
   dgas=Integer.parseInt(cgas.getText());
   dace=Integer.parseInt(cace.getText());
   total=dgas+dace+dres+dotr; 
   ctot.setText(" "+total); // sumo todoas los datos 
    
}
    private void ctotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ctotActionPerformed
     TotalSuma();
    }//GEN-LAST:event_ctotActionPerformed

    private void bresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bresActionPerformed
      cres.setEditable(true);
      cres.setText("");
    }//GEN-LAST:event_bresActionPerformed

    private void codvKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_codvKeyTyped
             char c = evt.getKeyChar();
       if(!(Character.isDigit(c))|| c== evt.VK_BACK_SPACE || c==evt.VK_DELETE){
           getToolkit().beep();
           evt.consume();
       }
    }//GEN-LAST:event_codvKeyTyped

    private void cgasKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cgasKeyTyped
         char c = evt.getKeyChar();
       if(!(Character.isDigit(c))|| c== evt.VK_BACK_SPACE || c==evt.VK_DELETE){
           getToolkit().beep();
           evt.consume();
       }
    }//GEN-LAST:event_cgasKeyTyped

    private void caceKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_caceKeyTyped
         char c = evt.getKeyChar();
       if(!(Character.isDigit(c))|| c== evt.VK_BACK_SPACE || c==evt.VK_DELETE){
           getToolkit().beep();
           evt.consume();
       }
    }//GEN-LAST:event_caceKeyTyped

    private void cresKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cresKeyTyped
          char c = evt.getKeyChar();
       if(!(Character.isDigit(c))|| c== evt.VK_BACK_SPACE || c==evt.VK_DELETE){
           getToolkit().beep();
           evt.consume();
       }
    }//GEN-LAST:event_cresKeyTyped

    private void cotrKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cotrKeyTyped
          char c = evt.getKeyChar();
       if(!(Character.isDigit(c))|| c== evt.VK_BACK_SPACE || c==evt.VK_DELETE){
           getToolkit().beep();
           evt.consume();
       }
    }//GEN-LAST:event_cotrKeyTyped
    public void Fecha(){
         // fecha actual 
        GregorianCalendar calendario = new GregorianCalendar();
     
//Hacer formateador de la fecha, se le pasa el formato en que se quiere obtener la fecha.
SimpleDateFormat df = new SimpleDateFormat("dd"); 
cdia.setText(""+df.format(calendario.getTime()));
df=new SimpleDateFormat("MM");
cmes.setText(""+df.format(calendario.getTime()));
df=new SimpleDateFormat("yyyy");
cano.setText(""+df.format(calendario.getTime()));

        

    }
    public void Insertar() throws FileNotFoundException{
     try {
            // en caso que haya cambiado la fecha   
      fecha=cano.getText().toString()+"-"+cmes.getText().toString()+"-"+cdia.getText().toString();
        //GASTOS
        

            int resultado = 0 ;

            MysqlDataSource dataSource = new MysqlDataSource();
            dataSource.setUser("root");
            dataSource.setPassword("");
            dataSource.setDatabaseName("practicasp8");
            dataSource.setServerName("localhost");
             // coneccion
            Connection conexion = (Connection) dataSource.getConnection();

            PreparedStatement ps = null;
            ps = (PreparedStatement) conexion.prepareStatement("insert into gasto (idvehiculo,gasolina,aceite,frenos,otrosgastos,total,descripcion,fecha,tipovehiculo,kilometraje) values (?,?,?,?,?,?,?,?,?,?)");

            ps.setInt(1,Integer.parseInt(codv.getText())); // con el ps genero lo qee voy a ingresar
            //lo genero en orden el 1 va a ser el dato uno en este caso seria nombre
            // y como nombre es varchar tonces sera string
            
            ps.setInt(2,dgas);
            ps.setInt(3,dace);
            ps.setInt(4,dres);
            ps.setInt(5,dotr);
            ps.setInt(6,total);
            ps.setString(7,cdes.getText());
            ps.setDate(8, Date.valueOf(fecha));
            ps.setString(9, tipov);
            ps.setInt(10, kilometraje);

            resultado = ps.executeUpdate();



               conexion.close(); // cierro la conexion

             }catch(SQLException e){

            System.out.println("EXCEPCION EN Base de Datos:"+e.toString());
           } // fin del catch
}
    /**
     *
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NuevoGasto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton bace;
    private javax.swing.JRadioButton bgas;
    private javax.swing.JRadioButton botr;
    private javax.swing.JRadioButton bres;
    private javax.swing.JLabel btot;
    private javax.swing.JTextField cace;
    private javax.swing.JTextField cano;
    private javax.swing.JTextArea cdes;
    private javax.swing.JTextField cdia;
    private javax.swing.JTextField cgas;
    private javax.swing.JTextField cmes;
    private javax.swing.JTextField codv;
    private javax.swing.JTextField cotr;
    private javax.swing.JTextField cres;
    private javax.swing.JTextField ctot;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
